<?php

namespace App\Controller\Admin\System;

use App\Controller\Admin\AdminController;
use App\Controller\Admin\BaseController;
use App\Dao\System\SystemRolesDao;
use App\Dao\System\SystemRolesMenuDao;
use App\Dao\System\SystemRouteDao;
use Hyperf\Di\Annotation\Inject;
use Yj\Apidog\Annotation\ApiController;
use Yj\Apidog\Annotation\FormData;
use Yj\Apidog\Annotation\PostApi;
use Yj\Form\Elm;

/**
 * @ApiController(prefix="admin/system/route")
 */
class SystemRouteController extends BaseController
{
    /**
     * @Inject()
     * @var SystemRouteDao
     */
    protected $systemRouteDao;

    /**
     * @PostApi(path="lists")
     */
    public function lists()
    {
        $params = $this->getValidatorData();
        dump($params);
        $data = $this->systemRouteDao->lists([]);
        _SUCCESS($data);
    }

    /**
     * @PostApi(path="form",description="任务表单")
     * @FormData(key="id",rule="")
     */
    public function Form()
    {
        $params = $this->getValidatorData();
        if($id = $params->get('id')){
            $formData =  $this->systemRolesDao->detail($id);
        }else{
            $formData = collect();
        }
        $form = Elm::createForm($id?'admin/system/roles/edit':'admin/system/roles/add',[],['labelPosition'=>'right']);
        $form->setRule([
            Elm::input('name','角色名称')->required()->col(12),
        ]);
        $id && $form->append(Elm::hidden('id',$id));
        $lists = $form->setTitle(is_null($id) ? '新增角色' : '编辑角色')->formData($formData->toArray());
        _SUCCESS(formToData($lists));
    }

    /**
     * @PostApi(path="add",description="添加角色")
     * @FormData(key="name",rule="required")
     */
    public function add(){
        $params = $this->getValidatorData();
        $this->systemRolesDao->add($params);
        _SUCCESS();
    }

    /**
     * @Inject()
     * @var SystemRolesMenuDao
     */
    protected $systemRolesMenuDao;

    /**
     * @PostApi(path="set-menus",description="设置角色菜单权限")
     * @FormData(key="roles_id",rule="required")
     * @FormData(key="menus_id",rule="required")
     */
    public function setMenus(){
        $params = $this->getValidatorData();
        $this->systemRolesMenuDao->setMenus($params->get('roles_id'),$params->get('menus_id'));
        _GetLastSql();
        _SUCCESS();
    }

    /**
     * @PostApi(path="delete",description="删除角色")
     * @FormData(key="id",rule="required")
     */
    public function delete(){
        $roles_id = $this->getValidatorData('id');
        $this->systemRolesDao->del($roles_id);
        _SUCCESS();
    }

}